﻿using Microsoft.EntityFrameworkCore;
using Polaris.Domain.Interfaces.Admin;
using Polaris.Domain.Model.Dto.Home.Request;
using Polaris.Domain.Model.Entity.Exception;
using Polaris.Domain.Repository.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Polaris.Infrastructure.DomainService.Admin
{
    public class HomeService : IHome
    {
        private readonly IBaseRepository<Domain.Model.DbEntity.WebSite> _webSite;

        public HomeService(IBaseRepository<Domain.Model.DbEntity.WebSite> webSite)
        {
            _webSite = webSite;
        }

        /// <summary>
        /// 新增收藏网址
        /// </summary>
        /// <param name="create"></param>
        /// <param name="userId"></param>
        /// <returns></returns>
        public async Task CreateWebSite(CreateWebSiteRequest create, Guid? userId)
        {
            try
            {
                var webSite = new Domain.Model.DbEntity.WebSite()
                {
                    Id = Guid.NewGuid(),
                    Enable = true,
                    CreateBy = userId,
                    CreatedTime = DateTime.Now,
                    SiteIcon = create.SiteIcon,
                    SiteName = create.SiteName,
                    SiteUrl = create.SiteUrl,
                    SiteIntro = create.SiteIntro,
                    SiteType = create.SiteType,
                };
                await _webSite.AddEntityAsync(webSite);
                await _webSite.CommitAsync();
            }
            catch (Exception ex)
            {
                throw new CustomException(Domain.Model.Enum.ResultCode.FAIL, ex.Message);
            }
        }

        /// <summary>
        /// 获取网址分类
        /// </summary>
        /// <returns></returns>
        public async Task<List<string>> GetWebSiteType()
        {
            var webList = await _webSite.QueryAll(d => !d.IsDelete).Select(d => d.SiteType).GroupBy(d => d).ToListAsync();
            var result = webList.Select(d => d.Key).ToList();
            return result;
        }
    }
}
